<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('提醒设置') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('事项类型')" prop="itemType">
        <el-select v-model="queryParams.itemType" size="small" filterable>
          <el-option
            v-for="(item, index) in itemTypeOptions"
            :key="index"
            :label="$t(item.dictLabel)"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('提醒频率')" prop="reminderFrequency">
        <el-select v-model="queryParams.reminderFrequency" size="small" filterable>
          <el-option
            v-for="(item, index) in reminderFrequencyOptions"
            :key="index"
            :label="$t(item.dictLabel)"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('状态')" prop="status">
        <el-select v-model="queryParams.status" size="small" filterable>
          <el-option :label="$t('开启')" value="1" />
          <el-option :label="$t('关闭')" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['msg:reminder:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="msgReminderSettingList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('事项类型')" align="center" prop="itemType" show-overflow-tooltip :formatter="itemTypeFormat" width="110" />
      <el-table-column :label="$t('项目/基金类型')" align="center" prop="projectTypeTranslate" show-overflow-tooltip />
      <el-table-column :label="$t('项目/基金名称')" align="center" prop="moduleName" show-overflow-tooltip />
      <el-table-column :label="$t('提醒标题')" align="center" prop="reminderTitle" show-overflow-tooltip />
      <el-table-column :label="$t('提醒对象类型')" align="center" prop="reminderObjectType" :formatter="reminderObjectTypeFormat" width="120" />
      <el-table-column :label="$t('提醒方式')" align="center" prop="reminderMode" show-overflow-tooltip :formatter="reminderModeFormat" width="150">
        <template slot-scope="scope">
          <span v-for="(item, index) in reminderModeFormat(scope.row)" :key="index">
            {{ $t(item) }}{{ index === reminderModeFormat(scope.row).length - 1 ? '' : ',' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('提醒频率')" show-overflow-tooltip align="center" prop="reminderFrequency" :formatter="reminderFrequencyFormat" width="110" />
      <el-table-column :label="$t('提醒状态')" show-overflow-tooltip align="center" prop="status" width="110">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="statusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['msg:reminder:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['msg:reminder:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改提醒设置对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <msgReminderSettingForm ref="msgReminderSettingForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listMsgReminderSetting, delMsgReminderSetting, updateStatus } from '@/api/oa/reminder/msgReminderSetting'
import msgReminderSettingForm from './msgReminderSettingForm.vue'
export default {
    components: {
        msgReminderSettingForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 提醒设置表格数据
          msgReminderSettingList: [],
          // 弹出层标题
          title: '提醒设置',
          // 是否显示弹出层
          formOpen: false,
        // 事项类型字典
        itemTypeOptions: [],
        // 项目/基金类型字典
        projectTypeOptions: [],
        // 提醒对象类型字典
        reminderObjectTypeOptions: [],
        // 提醒方式字典
        reminderModeOptions: [],
        // 提醒频率字典
        reminderFrequencyOptions: [],
          // 查询参数
          queryParams: {
              pageNum: 1,
              pageSize: 10,
              itemType: undefined,
              reminderFrequency: undefined,
              status: undefined
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
      this.getDicts('reminder_type').then(response => {
          this.itemTypeOptions = response.data
      })
      this.getDicts('reminder_object_type').then(response => {
          this.reminderObjectTypeOptions = response.data
      })
      this.getDicts('reminder_mode').then(response => {
          this.reminderModeOptions = response.data
      })
      this.getDicts('reminder_frequency').then(response => {
          this.reminderFrequencyOptions = response.data
      })
  },
  methods: {
    /** 查询提醒设置列表 */
    getList() {
        this.loading = true
        listMsgReminderSetting(this.queryParams).then(response => {
            this.msgReminderSettingList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 事项类型字典翻译
    itemTypeFormat(row) {
      return this.$t(this.selectDictLabel(this.itemTypeOptions, row.itemType))
    },
    // 提醒对象类型字典翻译
    reminderObjectTypeFormat(row) {
      return this.$t(this.selectDictLabel(this.reminderObjectTypeOptions, row.reminderObjectType))
    },
    // 提醒方式字典翻译
    reminderModeFormat(row) {
      const array = []
      row.reminderMode.forEach((e) => {
        array.push(this.$t(this.selectDictLabel(this.reminderModeOptions, e)))
      })
      return array
    },
    // 提醒频率字典翻译
    reminderFrequencyFormat(row) {
      return this.$t(this.selectDictLabel(this.reminderFrequencyOptions, row.reminderFrequency))
    },
    // 项目/基金类型翻译
    // projectType(row) {
    //     this.getDicts('reminder_project_type').then(response => {
    //       this.projectTypeOptions = response.data
    //     })
    //   return this.$t(this.selectDictLabel(this.projectTypeOptions, row.projectType))
    // },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            itemType: undefined,
            moduleId: undefined,
            reminderTitle: undefined,
            reminderObjectType: undefined,
            reminderObject: undefined,
            reminderMode: undefined,
            reminderFrequency: '0',
            timerReminderTime: undefined,
            frequencyCronValue: undefined,
            cron: undefined,
            status: '0',
            reminderContent: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.msgReminderSettingForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.msgReminderSettingForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delMsgReminderSetting(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    statusChange(row) {
      const status = row.status === 1 ? '开启' : '关闭'
      this.$confirm(this.$t('确定修改当前提醒状态') + this.$t(status) + '？', this.$t('提示'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        dangerouslyUseHTMLString: true,
        type: 'info'
      }).then(() => {
        const params = {
          id: row.id,
          status: row.status
        }
        updateStatus(params).then(response => {
          if (response.code === 200) {
            this.msgSuccess(this.$t('修改成功'))
          }
        })
      }).catch(() => {
        row.status = row.status === 1 ? 0 : 1
        this.$message({
          type: 'info',
          message: this.$t('已取消修改')
        })
      })
    }
  }
}
</script>
